//
//  AppDelegate.m
//  AIM
//
//  Created by user10 on 2017/5/15.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "AppDelegate.h"
#import "StartViewController.h"
#import "ViewController.h"
#import "MainNavigationController.h"
#import "UDPManage.h"
#import "DataBase.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]] ;
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次启动");
        //如果是第一次启动的话,使用UserGuideViewController (用户引导页面) 作为根视图
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        StartViewController *userGuideViewController = [story instantiateViewControllerWithIdentifier:@"first"];
        self.window.rootViewController = userGuideViewController;
    }
    else
    {
        NSLog(@"不是第一次启动");
        //在用户偏好中取是否登陆过的字符串
        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
        NSString *userLoginStr = [user objectForKey:@"login"];
        //如果登陆过用mainNavController作为主控制器
        if ([userLoginStr containsString:@"SUCCESS"]) {
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UINavigationController * mainVc = [story instantiateViewControllerWithIdentifier:@"mainNavController"];
            self.window.rootViewController = mainVc;
        }else if(userLoginStr == NULL){
            //没登录过用TranslateController作为主控制器
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UINavigationController * vc = [story instantiateViewControllerWithIdentifier:@"TranslateController"];
            self.window.rootViewController = vc;
        }
    }
    [[DataBase sharedDataBase]delete30Day];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UDPClose" object:self];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UDPReconnection" object:self];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Foreground" object:self];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



@end
